package com.guigu.hkw.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashSet;
import java.util.Set;

public class UserRealm extends AuthorizingRealm {
    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String s = principalCollection.getPrimaryPrincipal().toString();

        //如果当前用户是 siniu 就他一个a角色，其他用户就是b角色
        Set<String> strings = new HashSet<String>();
      Set<String> permissions= new HashSet<String>();
        if (s.equals("a")){
            strings.add("a");
            permissions.add("user:del");
            permissions.add("user:find");
        }
         strings.add("b");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(strings);
        info.setRoles(strings);//设置用户角色
        info.setStringPermissions(permissions);//设置用户权限

        return info;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取当前账号
        String principal = (String) authenticationToken.getPrincipal();
        System.out.println(principal);
        //获取盐值
        ByteSource bytes = ByteSource.Util.bytes(principal);
        //获取当前密码
        SimpleAuthenticationInfo simp=new SimpleAuthenticationInfo(principal,"123",getName());
        return simp;
    }

    public static void main(String[] args) {//盐值
        //salt在里面放用户名
        Object simpleHash = new SimpleHash("MD5", "123","a",100000);
        System.out.println(simpleHash);
    }
}
